package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.utils.FFmpegUtils;
import com.yc.cloud.common.basic.api.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 视频处理控制器
 * 
 * @author kinggu
 * @since 2025-01-XX
 */
@RestController
@RequestMapping("/video")
@Tag(name = "视频处理")
public class VideoProcessController {

    @Resource
    private FFmpegUtils ffmpegUtils;

    @PostMapping("/capture-frame")
    @Operation(summary = "截取视频帧作为封面图")
    public ResponseResult<String> captureFrame(
            @Parameter(description = "视频文件路径", required = true) 
            @RequestParam String videoPath,
            @Parameter(description = "输出文件路径，为空时自动生成") 
            @RequestParam(required = false) String outputPath,
            @Parameter(description = "截取时间位置（秒），为空时选择视频中间位置") 
            @RequestParam(required = false) Double timePosition) {
        
        String result = ffmpegUtils.captureFrame(videoPath, outputPath, timePosition);
        if (result != null) {
            return ResponseResult.success(result);
        } else {
            return ResponseResult.failed("视频截图失败");
        }
    }

    @GetMapping("/duration")
    @Operation(summary = "获取视频时长")
    public ResponseResult<Double> getVideoDuration(
            @Parameter(description = "视频文件路径", required = true) 
            @RequestParam String videoPath) {
        
        Double duration = ffmpegUtils.getVideoDuration(videoPath);
        if (duration != null) {
            return ResponseResult.success(duration);
        } else {
            return ResponseResult.failed("获取视频时长失败");
        }
    }

    @GetMapping("/ffmpeg-status")
    @Operation(summary = "检查FFmpeg是否可用")
    public ResponseResult<Boolean> checkFFmpegStatus() {
        boolean available = ffmpegUtils.isFFmpegAvailable();
        return ResponseResult.success(available);
    }
}
